package com.cpi.cornpp.transports.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.corntypes.services.CornTypesService;
import com.cpi.cornpp.transports.comparator.CostElementAddedComparator;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportChargeView;
import com.cpi.cornpp.transports.view.TransportView;

public class ShowEditTransportAction extends Action {
	
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String FAILURE_FORWARD = "error";
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		String messageError = null;
		TransportForm transportForm = null;
		TransportView transportView = null;
		TransportChargeView transportChargeView = null;
		TransportService transportService = null;
		CornTypesService cornTypesService = null;
		CostElementView costElementView = null;
		List costElementsView = null;
		List transportChargesView = null;
		List cornTypesList = null;
		HttpSession session = null;
		ActionForward forward = null;
		MessageResources messageResources = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		messageResources = getResources(request);
		
		try{
			
			if (form != null){
				transportForm = (TransportForm) form;
				transportView = (TransportView) session.getAttribute("transportView");
				cornTypesList = new ArrayList(0);
				
				if ((transportView == null) && (StringUtils.isNotEmpty(transportForm.getId()))){
					transportService = new TransportService();					
					
					transportView = transportService.getTransport(Integer.parseInt(transportForm.getId()));
										
					if (transportView != null){
						transportChargesView = transportService.getTransportChargesByTransport(transportView.getTransportIdentifier());
						costElementsView = transportService.getCostElements();
						
						if (CollectionUtils.isNotEmpty(transportChargesView)){
							
							for (Iterator itrTransportCharge = transportChargesView.iterator(); itrTransportCharge.hasNext(); ){
								transportChargeView = (TransportChargeView) itrTransportCharge.next();
								
								for (Iterator itrCostElement = costElementsView.iterator(); itrCostElement.hasNext(); ){
									costElementView = (CostElementView) itrCostElement.next();
									
									if (transportChargeView.getTransportChargeTypeId() == costElementView.getChargeTypeId()){
										costElementView.setChargeTypeCost(transportChargeView.getTransportChargeAmount());
										transportView.getTransportCostElements().add(costElementView);
										break;
									}
								}
							}
						}
						
						session.removeAttribute("deletedTransportCostElements");
						session.removeAttribute("addedTransportCostElements");
						
					} else {
						TransportException tre = new TransportException("El transporte seleccionado no existe", TransportException.LAYER_ACTION, TransportException.ACTION_SELECT);
						
						throw tre;
					}
				} else {
					
				}
				Collections.sort(transportView.getTransportCostElements(), new CostElementAddedComparator());				
				cornTypesService = new CornTypesService();
				cornTypesList = cornTypesService.getListCornTypes();
				session.setAttribute("cornTypesList", cornTypesList);
				session.setAttribute("transportView", transportView);
				request.setAttribute("transportView", transportView);
				
				forward = mapping.findForward(SUCCESS_FORWARD);
			}
			
		} catch(TransportException tre){
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), tre.getErrorCode());
			
			if(null == messageError){
				session.setAttribute(CUSTOM_EXCEPTION, tre);
				form.reset(mapping, request);
				throw tre;
			}else{
				session.setAttribute(ERROR_MESSAGE, messageError);
				forward = mapping.findForward(FAILURE_FORWARD);
			}
			
		} catch(Exception e){
			TransportException tre = new TransportException(e , TransportException.LAYER_ACTION, TransportException.ACTION_INSERT);;
			
			session.setAttribute(CUSTOM_EXCEPTION, tre);
			form.reset(mapping, request);
			e.printStackTrace(System.out);
			
			throw tre;
		}
		
		return forward;
	}
	
}